<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <div class="my-channel channel">
      <van-cell title="我的频道" :border="false">
        <!-- 标题/编辑的插槽 -->
        <template>
          <van-button @click="isEdit= !isEdit" color="crimson" plain round size="mini">
            {{ isEdit ?'完成' :'编辑' }}
          </van-button>
        </template>
      </van-cell>
      <!-- 我的频道 列表 -->
      <van-grid :gutter="10">
        <van-grid-item v-for="(item,index) in myChannels" :key="item.id"
          :text="item.name"
          :icon="(isEdit && index!==0) ?'close' :'' "
          :class="{active:index===activeIndex}"
          @click="onMyItem(index,item.id)"
        />
      </van-grid>
    </div>

    <!-- 推荐频道 -->
    <div class="recommend-channel channel">
      <van-cell title="推荐频道" :border="false"/>
      <!-- 推荐频道 列表 -->
      <van-grid :gutter="10" icon-size="0.32rem" direction="horizontal" >
        <van-grid-item icon="plus" v-for="item in recommendChannel" :key="item.id"
          :text="item.name"
          @click="addToMyChannels(item)"
        />
      </van-grid>
    </div>
  </div>
</template>

<script>
import { getAllChannel, addUserChannel, deleteUserChannel } from '@/api/channel.js'
import { Toast } from 'vant'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'
export default {
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    activeIndex: {
      type: Number,
      require: true
    }
  },
  data () {
    return {
      allchannel: [],
      isEdit: false
    }
  },

  created () {
    this.loadAllChannel()
  },

  computed: {
    recommendChannel () {
      return this.allchannel.filter(channelItem => {
        return !this.myChannels.some(myItem => myItem.id === channelItem.id)
      })
    },
    ...mapState(['user'])
  },

  methods: {
    // 加载所有频道列表
    async loadAllChannel () {
      try {
        const { channels } = await getAllChannel()
        this.allchannel = channels
      } catch (err) {
        Toast('异常')
      }
    },

    // 点击推荐频道 添加到 我的频道中
    async addToMyChannels (it) {
      this.myChannels.push(it)
      // 判断用户是否登录
      if (this.user) {
        // 登陆时 存数据,发请求
        try {
          await addUserChannel({
            id: it.id,
            seq: this.myChannels.length
          })
          Toast('添加成功')
        } catch (err) {
          Toast('添加失败')
        }
      } else {
        // 未登录时 存本地
        setItem('HMTT', this.myChannels)
      }
    },

    // 点击我的频道 删除/跳转
    onMyItem (index, id) {
      if (this.isEdit) {
        if (index === 0) return
        if (index <= this.activeIndex) {
          this.$emit('update-active', this.activeIndex - 1, true)
        }
        // 删除
        this.myChannels.splice(index, 1)
        // 删除持久化
        this.deleteStorage(id)
      } else {
        // 跳转
        this.$emit('update-active', index, false)
      }
    },
    // 删除持久化
    deleteStorage (id) {
      // 判断用户是否登录
      if (this.user) {
        // 登陆时 发请求
        try {
          deleteUserChannel(id)
          Toast.success('删除成功')
        } catch (err) {
          Toast.fail('删除失败')
        }
      } else {
        // 未登录时 存本地
        setItem('HMTT', this.myChannels)
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit{
  padding-top: 90px;
  .channel{
    /deep/.van-grid{
      .van-grid-item__content{
        width: 160px;
        height: 86px;
        background: #f4f5f6;
        border-radius: 6px;
        .van-grid-item__text{
          font-size: 28px;
          color: #222;
        }
      }
      .van-grid-item_content::after{
        border: none;
      }
    }
  }
  /deep/.my-channel{
    .van-grid-item__icon+.van-grid-item__text{
      margin-top: 0;
    }
    .van-cell{
      display: flex;
      align-items: center;
      .van-button{
        width: 100px;
        height: 50px;
      }
    }
    .van-icon-close{
      position: absolute;
      right: -10px;
      top: -10px;
      font-size: 32px;
    }
    .active{
      .van-grid-item__content{
        .van-grid-item__text{
          color: crimson;
        }
      }
    }
  }
}
</style>
